//------------------------------------------------------------------------------
//!	@file xspv.c
//!	@brief
//!		xspv main file
//!
//!	$Id: $
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//	original author: Greg Baumont - http://greg-baumont.com
//
//	This file is part of XSPV: Oberheim Xpander Single Patch Viewer.
//
//	XSPV is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	XSPV is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with XSPV.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <stdint.h>																//	Standard int types
#include <stdbool.h>															//	Bool

#include <popt.h>

#include "bi.h"
#include "XpanderSysEx.h"

//------------------------------------------------------------------------------
// Local defines
//------------------------------------------------------------------------------
#define OPT_INPUT			0x0001
#define OPT_OUTPUT			0x0002
#define OPT_ABOUT			0x0100
#define OPT_VERBOSE			0x0200

//------------------------------------------------------------------------------
// Local data
//------------------------------------------------------------------------------
static char*			FileName_In;	//  FileName: Input
static FILE*			r_file;			//	read file
static char*			FileName_Out;	//  Filename: Output
static poptContext		optCon;

//------------------------------------------------------------------------------
// Main function
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	uint32_t	opt=0;
	int			idx=0;
	SinglePatch pXp;

	struct poptOption optionsTable[] = {
		{ "in",		'i', POPT_ARG_STRING, &FileName_In,	 'i', "input filename (required)",			NULL	},
		{ "out",	'o', POPT_ARG_STRING, &FileName_Out, 'o', "output filename (none = stdout)",	NULL	},
		{ "about",	'a', POPT_ARG_NONE,	  NULL,			 'a', "display information about the app",	NULL	},
		{ "verbose",'v', POPT_ARG_NONE,	  NULL,			 'v', "enable verbose mode",				NULL	},
		POPT_AUTOHELP
		POPT_TABLEEND
	};

//------------------------------------------------------------------------------
//  Process arguments
//------------------------------------------------------------------------------
	optCon = poptGetContext(NULL, argc, (const char **)argv, optionsTable, 0);
	poptSetOtherOptionHelp(optCon, "[OPTIONS]*");
	if (argc == 1) {
		poptPrintHelp(optCon, stdout, 0);
		exit(1);
	}
	
	while ((idx = poptGetNextOpt(optCon)) >= 0) {
		switch (idx) {
		case 'i':
			opt |= OPT_INPUT;
			break;
		case 'o':
			opt |= OPT_OUTPUT;
			break;
		case 'a':
			opt |= OPT_ABOUT;
			break;
		case 'v':
			opt |= OPT_VERBOSE;
			break;
		}
	}
	if (idx < -1) {
		fprintf(stderr, "%s: %s\n",
		        poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		        poptStrerror(idx));
		exit(opt);
	}
	if (opt & OPT_ABOUT) {
		bldinfoPrintBldInfo();
		exit(0);
	}

//------------------------------------------------------------------------------
//  Open the file
//	Read the patch
//	Dump the patch
//------------------------------------------------------------------------------
	r_file = fopen(FileName_In, "rb");											// Open input file
	if (r_file == NULL) {														// Can't open it
		printf("Cannot open %s\n", FileName_In);								// Let'm know
		exit(1);																// bail
	}

	LocateSinglePatchData(r_file);												// Locate the start of the patch
	ReadSinglePatchData(r_file, &pXp);											// Read a patch
	DumpPatch(&pXp);															// Dump the patch

	fclose(r_file);																// Close the file

	return 0;
}

//
//  End: xspv.c
//
